<?php
/**
 * Html-content edit tool
 * 
 * @author MF
 * @version 2.0 
 */

require_once 'Zend/Controller/Action.php';

class Cms_HtmlController extends Zend_Controller_Action {
	
	

	public function indexAction() {
		CmsAuth::check();
		throw new Zend_Controller_Action_Exception('Index not set', 404);
	}
	
	public function editAction() {
		CmsAuth::check();
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$this->view->mode = $this->getRequest()->getParam('mode');
		
		try {
			$getHtml = MF_Main::getInstance()->Html->getById($id); 
			
			if ($this->_request->isPost()) {
				$post_data = $this->_request->getPost();
				//$html = ereg_replace("^.*<body(.*)</body>.*$","\\1",$post_data['data']);
				$html = $post_data['data'];
				/* $html_begin = strpos($html,'<body');
				$html = $html_begin===FALSE ? $html : substr($html,$html_begin,strlen($html));
				$html_begin_end = strpos($html,'>');
				$html = $html_begin_end===FALSE ? $html : substr($html,$html_begin_end+1,strlen($html));
				$html_end = strpos($html,'</body');
				$html = $html_end===FALSE ? $html : substr($html,0,$html_end); */
				$getHtml->data = $html;
				$this->renderScript('exit.phtml');
				return true;
			} else {
				$this->view->data = $getHtml->data;
			}
		} catch (PushError $e) {
			$this->view->error_message = $e->getUserMessage();
			$this->renderScript('fatal.phtml');
			return false;
		}
		
		
	}
	
	public function filledAction() {
		CmsAuth::check();
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		try {
			$getHtml = MF_Main::getInstance()->Html->getById($id); 
		} catch (PushError $e) {
			$this->view->error_message = $e->getUserMessage();
			$this->renderScript('fatal.phtml');
			return false;
		}
		
		$this->view->result = !$getHtml->data ? '0' : '1';
		$this->_helper->layout->disableLayout();
	}
	
	
}
?>